package com.ruoyi.framework.web.service;

import com.ruoyi.common.core.domain.DTO.InsertUserBankAccountBO;
import com.ruoyi.common.core.domain.DTO.InsertUserZfbAccountBO;
import com.ruoyi.common.core.domain.DTO.UpdateUserBankAccountBO;
import com.ruoyi.common.core.domain.DTO.UpdateUserZfbAccountBO;
import com.ruoyi.common.core.domain.entity.UserBankAccount;
import com.ruoyi.common.core.domain.entity.UserZfbAccount;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.mapper.UserBankAccountMapper;
import com.ruoyi.system.mapper.UserZfbAccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
@Transactional
public class UserZfbAccountService {
    @Autowired
    private UserZfbAccountMapper userZfbAccountMapper;

    public void insertUserZfbAccount(InsertUserZfbAccountBO insertUserZfbAccountBO) {
        if (insertUserZfbAccountBO.getIsDefault() == 1) {
            UserZfbAccount defaultUserZfbAccount = userZfbAccountMapper.selectDefaultByUserId(insertUserZfbAccountBO.getUserId());
            if (!ObjectUtils.isEmpty(defaultUserZfbAccount)) {
                //先取消该用户的默认账户
                userZfbAccountMapper.cancelDefaultByUserId(insertUserZfbAccountBO.getUserId());
            }
        }
        UserZfbAccount userZfbAccount = new UserZfbAccount();
        BeanUtils.copyProperties(insertUserZfbAccountBO, userZfbAccount);
        userZfbAccount.setStatus(1);
        userZfbAccount.setCreateTime(new Date());
        userZfbAccount.setUpdateTime(new Date());
        userZfbAccountMapper.insertUserZfbAccount(userZfbAccount);
    }

    public void updateUserZfbAccount(UpdateUserZfbAccountBO updateUserZfbAccountBO) {
        if(updateUserZfbAccountBO.getIsDefault()==1){
            //先取消该用户的默认账户
            userZfbAccountMapper.cancelDefaultByUserId(updateUserZfbAccountBO.getUserId());
        }
        UserZfbAccount userZfbAccount = new UserZfbAccount();
        BeanUtils.copyProperties(updateUserZfbAccountBO, userZfbAccount);
        userZfbAccount.setUpdateTime(new Date());
        userZfbAccountMapper.updateUserZfbAccount(userZfbAccount);
    }

    public List<UserZfbAccount> selectUserZfbAccountByUserId(Long userId) {
        return userZfbAccountMapper.selectUserZfbAccountByUserId(userId);
    }

    public UserZfbAccount selectUserZfbAccountById(Long id) {
        return userZfbAccountMapper.selectUserZfbAccountById(id);
    }
}
